package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.SupplierMapper;
import com.example.model.dtos.PageResult;
import com.example.model.pojo.Supplier;

import com.example.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements SupplierService {

    @Autowired(required = false)
    SupplierMapper supplierMapper;

    @Override
    public Boolean updateSupplier(Supplier supplier) {
        int insert = supplierMapper.updateById(supplier);
        return insert != 0;
    }

    @Override
    public PageResult SupplierList(Integer pageStart, Integer pageSize) {
        //创建分页对象并将当前页码和页面大小写入
        Page<Supplier> supplierPage =new Page<>(pageStart, pageSize);
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        supplierMapper.selectPage(supplierPage, queryWrapper);
        List<Supplier> records = supplierPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), supplierPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public Boolean deleteSupplier(String id) {
        int insert = supplierMapper.deleteById(id);
        return insert != 0;
    }

    @Override
    public Boolean addSupplier(Supplier supplier) {
        int insert = supplierMapper.insert(supplier);
        return insert != 0;
    }

    @Override
    public List<Supplier> getAll() {
        List<Supplier> supplierList = supplierMapper.selectList(new QueryWrapper<>());
        return supplierList;
    }
}
